package com.gufe.controller.admin.mmp;

import com.gufe.entity.Notice;
import com.gufe.result.PageResult;
import com.gufe.result.Result;
import com.gufe.service.admin.mmp.IndexNoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/admin/mp")
/*管理端小程序通知*/
public class IndexNoticeController {

    @Autowired
    IndexNoticeService indexNoticeService;

    @GetMapping("/notice/page")
    /*通知内容分页查询*/
    //@PreAuthorize("hasRole('ADMIN')")
    @Cacheable(cacheNames = "noticeCache",key = "'getPageNotice:page=' + #page + ',pageSize=' + #pageSize")
    public Result<PageResult> getPageNotice(@RequestParam Integer page, @RequestParam Integer pageSize) {
        log.info("分页查询通知第{}页{}条数据！",page,pageSize);
        PageResult noticePageVO = indexNoticeService.getPageNotice(page,pageSize);
        return Result.success(noticePageVO);
    }

    /*新增通知*/
    @PostMapping("/notice")
    @CacheEvict(cacheNames = "noticeCache",allEntries = true) // 清除所有通知缓存
    public Result<String> addNotice(@RequestBody Notice notice) {
        log.info("添加通知内容：{}",notice);
        indexNoticeService.addNotice(notice);
        return Result.success();
    }

    //根据id删除通知
    @DeleteMapping("/notice")
    @CacheEvict(cacheNames = "noticeCache",allEntries = true) // 清除所有通知缓存
    public Result deleteNoticeById(@RequestParam Integer id) {
        log.info("删除通知id：{}",id);
        indexNoticeService.deleteNoticeById(id);
        return Result.success();
    }

    /*修改通知内容*/
    @PutMapping("/notice")
    @CacheEvict(cacheNames = "noticeCache",allEntries = true) // 清除所有通知缓存
    public Result updateNotice(@RequestBody Notice notice) {
        log.info("修改通知内容：{}",notice);
        indexNoticeService.updateNotice(notice);
        return Result.success();
    }

}
